package com.augurit.gzcgwsp.printDoc.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.NumberFormats;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.system.web.auth.form.LoginUserForm;
import com.augurit.common.util.PageUtils;
import com.augurit.gzcgwsp.commonex.util.lang.DateUtilsEX;
import com.augurit.gzcgwsp.csshlj.entity.Csshljcz;
import com.augurit.gzcgwsp.csshlj.service.ICsshljCzService;
import com.augurit.gzcgwsp.csshlj.service.IShljYsService;
import com.augurit.gzcgwsp.excelimportexport.util.ExcelUtil;
import com.augurit.gzcgwsp.excelimportexport.util.FileUtil;
import com.augurit.gzcgwsp.excelimportexport.util.GetFileName;
import com.augurit.gzcgwsp.hwgz.entity.YwHwShSqb;
import com.augurit.gzcgwsp.hwgz.service.IYwHwShSqbService;
import com.augurit.gzcgwsp.jzfqwczz.entity.YwYnJzfqwczcJbxx;
import com.augurit.gzcgwsp.jzfqwczz.service.IYwYnJzfqwczcBghzService;
import com.augurit.gzcgwsp.jzfqwczz.service.IYwYnJzfqwczcJbxxService;
import com.augurit.gzcgwsp.jzfqwczz.service.IYwYnJzfqwczcNsxzService;
import com.augurit.gzcgwsp.jzfqwczz.service.IYwYnJzfqwczcShhzService;
import com.augurit.gzcgwsp.jzfqwczz.service.IYwYnJzfqwczcYsbbService;
import com.augurit.gzcgwsp.jzfqwczzpfsn.entity.YwYnJzfqwczzPfsn;
import com.augurit.gzcgwsp.jzfqwczzpfsn.service.IYwYnJzfqwczzPfsnService;
import com.augurit.gzcgwsp.jzfqwczzpfsn.service.IYwYnJzfqwczzPfsnbgService;
import com.augurit.gzcgwsp.jzfqwczzpfsn.service.IYwYnJzfqwczzPfsnyqService;
import com.augurit.gzcgwsp.jzfqwczzxn.entity.YwYnJzfqwczzXn;
import com.augurit.gzcgwsp.jzfqwczzxn.service.IYwYnJzfqwczzXnService;
import com.augurit.gzcgwsp.jzfqwczzxn.service.IYwYnJzfqwczzXnbgService;
import com.augurit.gzcgwsp.jzfqwczzxn.service.IYwYnJzfqwczzXnyqService;
import com.augurit.gzcgwsp.printDoc.convert.YwPrintDocConvertor;
import com.augurit.gzcgwsp.printDoc.dao.YwPrintDocDao;
import com.augurit.gzcgwsp.printDoc.entity.YwPrintDoc;
import com.augurit.gzcgwsp.printDoc.service.IYwPrintDocService;
import com.augurit.gzcgwsp.printDoc.web.form.YwPrintDocForm;
import com.augurit.gzcgwsp.qyzz.entity.YwRqQyzzInfo;
import com.augurit.gzcgwsp.qyzz.service.IYwRqQyzzInfoService;
import com.augurit.gzcgwsp.rqqyjyxk.entity.YwRqQyjyxk;
import com.augurit.gzcgwsp.rqqyjyxk.service.IYwRqQyjyxkService;
import com.augurit.gzcgwsp.sendNotice.service.IExchangeStateService;
import com.augurit.gzcgwsp.sendNotice.service.IYwMtbjlshInfoService;
import com.augurit.gzcgwsp.smartformCsdlwj.entity.YwGgcs;
import com.augurit.gzcgwsp.smartformCsdlwj.entity.YwSrCsdlwj;
import com.augurit.gzcgwsp.smartformCsdlwj.entity.YwSrDlzy;
import com.augurit.gzcgwsp.smartformCsdlwj.service.IYwCsdlwjService;
import com.augurit.gzcgwsp.smartformCsdlwj.service.IYwDlzyService;
import com.augurit.gzcgwsp.smartformCsdlwj.service.IYwGgcsServices;
import com.augurit.gzcgwsp.smartformCsdlwj.service.IYwShljqsService;
import com.augurit.gzcgwsp.ssgd.entity.YwRqSsgdSqb;
import com.augurit.gzcgwsp.ssgd.service.IYwRqSsgdSqbService;
import com.augurit.gzcgwsp.wfex.app.doc.IOaDocCodeServiceEX;
import com.augurit.gzcgwsp.wfex.common.service.IWfServiceEX;
import com.augurit.gzcgwsp.wfex.common.util.WfUtilsEX;
import com.augurit.wf.bi.web.form.WfTemplateAttachmentRefForm;
import com.augurit.wf.common.dao.Jbpm4HistProcinstDao;
import com.augurit.wf.common.dao.WfBusInstanceDao;
import com.augurit.wf.common.entity.Jbpm4HistProcinst;
import com.augurit.wf.common.entity.Jbpm4Task;
import com.augurit.wf.common.entity.WfBusInstance;
import com.augurit.wf.common.entity.WfBusTemplate;
import com.augurit.wf.common.entity.WfTemplateAttachmentRef;

@Service
@Transactional
public class YwPrintDocServiceImpl implements IYwPrintDocService {
	private static final Logger log=Logger.getLogger(YwPrintDocServiceImpl.class);
	
	@Autowired
	private WfBusInstanceDao wfBusInstanceDao;
	@Autowired
	private YwPrintDocDao ywPrintDocDao;
	  @Autowired
    private IYwMtbjlshInfoService ywMtbjlshInfoService;
	@Autowired
	private IYwRqQyjyxkService ywRqQyjyxkService;
	@Autowired
	private Jbpm4HistProcinstDao jbpm4HistProcinstDao;
	@Autowired
	private IExchangeStateService exchangeStateService;
	@Autowired
	private IYwRqSsgdSqbService ywRqSsgdSqbService;
	@Autowired
	private IWfServiceEX wfServiceEX;
	@Autowired
	private IYwRqQyzzInfoService ywRqQyzzInfoService;	
	@Autowired
	private IYwHwShSqbService ywHwShSqbService;	
	@Autowired
	private IYwDlzyService ywDlzyService;	
	@Autowired
	private IYwGgcsServices ywGgcsServices;	
	@Autowired
	private IYwCsdlwjService ywCsdlwjService;    
    @Autowired
    private IYwShljqsService ywShljqsService;    
    @Autowired
    private IShljYsService shljYsService;    
    @Autowired
    private ICsshljCzService csshljCzService;    
    @Autowired
    private IYwYnJzfqwczzPfsnService ywYnJzfqwczzPfsnService;    
    @Autowired
    private IYwYnJzfqwczzPfsnyqService ywYnJzfqwczzPfsnyqService;    
    @Autowired
    private IYwYnJzfqwczzPfsnbgService ywYnJzfqwczzPfsnbgService;    
    @Autowired
    private IYwYnJzfqwczzXnService ywYnJzfqwczzXnService;    
    @Autowired
    private IYwYnJzfqwczzXnbgService ywYnJzfqwczzXnbgService;    
    @Autowired
    private IYwYnJzfqwczzXnyqService ywYnJzfqwczzXnyqService;
    @Autowired
    private IYwYnJzfqwczcBghzService ywYnJzfqwczcBghzService;
    @Autowired
    private IYwYnJzfqwczcJbxxService ywYnJzfqwczcJbxxService;
    @Autowired
    private IYwYnJzfqwczcNsxzService ywYnJzfqwczcNsxzService;
    @Autowired
    private IYwYnJzfqwczcShhzService ywYnJzfqwczcShhzService;
    @Autowired
    private IYwYnJzfqwczcYsbbService ywYnJzfqwczcYsbbService;
    @Autowired
    private  IOaDocCodeServiceEX oaDocCodeServiceEX;
	public List<YwPrintDocForm> get(String templateCode, String fileCode,
			Long ywId) {
		String hql = "from YwPrintDoc t where t.templateCode =:templateCode and t.fileCode=:fileCode and t.ywId =:ywId";
	
		//初始化相应的条件参数
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("templateCode", templateCode);
		params.put("fileCode", fileCode);
		params.put("ywId", ywId);
		List<YwPrintDoc> ywPrintDocList = ywPrintDocDao.find(hql, params);		
		return YwPrintDocConvertor.convertVoListToFormList(ywPrintDocList);
	}

	public List<YwPrintDocForm> getNoFcode(String templateCode, Long ywId){
		String hql = "from YwPrintDoc t where t.templateCode =:templateCode and t.ywId =:ywId";
		
		//初始化相应的条件参数
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("templateCode", templateCode);
		params.put("ywId", ywId);
		List<YwPrintDoc> ywPrintDocList = ywPrintDocDao.find(hql, params);		
		return YwPrintDocConvertor.convertVoListToFormList(ywPrintDocList);
	}

	public Page<YwPrintDocForm> search(Page<YwPrintDocForm> page,
			List<PropertyFilter> filters) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page<YwPrintDocForm> search(Page<YwPrintDocForm> page,
			YwPrintDocForm form) {
		// TODO Auto-generated method stub
		return null;
	}

	public void delete(Long... ids) {
		// TODO Auto-generated method stub

	}

	public YwPrintDocForm get(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	public void save(YwPrintDocForm... forms) {
		if(forms != null)
			for(YwPrintDocForm form : forms){
				YwPrintDoc ywPrintDoc = new YwPrintDoc();
				YwPrintDocConvertor.convertFormToVo(form, ywPrintDoc);
				String templateCode=ywPrintDoc.getTemplateCode();
				Long ywId=ywPrintDoc.getYwId();
				String fileCode=ywPrintDoc.getFileCode();
				Long ywid=ywPrintDoc.getYwId();
				String sql=" from YwPrintDoc where templateCode='"+templateCode+"'"+" and ywId="+ywId+
						" and fileCode='"+fileCode+"'";
				//避免重复保存
				if(this.ywPrintDocDao.find(sql).size()<1){
					ywPrintDocDao.save(ywPrintDoc);
				}
			}
		
	}
	
	
	/**
	 *根据业务id查找
	 */
	public List<YwPrintDocForm> getByYwid(Long ywId,String tempcode) {
		String hql="from YwPrintDoc doc where doc.ywId="+ywId+" and templateCode='"+tempcode+"'  order by doc.id desc";
		List<YwPrintDoc> ywPrintDocList = ywPrintDocDao.find(hql);		
		return YwPrintDocConvertor.convertVoListToFormList(ywPrintDocList);
	}
	
	/**
	 * 根据id查找
	 */
    public YwPrintDocForm getFormById(Long id) {
        YwPrintDocForm form=null;
        String hql="from YwPrintDoc doc where doc.id="+id;
        List<YwPrintDoc> ywPrintDocList = ywPrintDocDao.find(hql);        
        List<YwPrintDocForm> list=YwPrintDocConvertor.convertVoListToFormList(ywPrintDocList);
        if(list!=null && list.size()>0){
            form=list.get(0);
        }
        return form;
    }

	
	/**
	 * 
	 */
    public WfTemplateAttachmentRefForm updateRefForm(YwPrintDocForm form,String templateId,String userName) {
        if(StringUtils.isNotBlank(templateId) && form.getId()!=null){
            String sql=" from "+YwPrintDoc.class.getName()+" where id='"+form.getId()+"'";
            List<YwPrintDoc> list = ywPrintDocDao.find(sql);  
            if(list!=null && list.size()>0){
                YwPrintDoc doc=list.get(0);
                if(doc.getTempdocId()==null){
                    WfTemplateAttachmentRef attref=this.getAttach(form, templateId);
                    if(attref!=null && attref.getId()!=null){
                       doc.setTempdocId(attref.getId());
                    }
                }
                if(doc.getCreatDate()==null){
                    doc.setCreatDate(new Date());
                }
                if(StringUtils.isBlank(doc.getCreator())){
                    doc.setCreator(userName);
                }
                //YwPrintDocForm docForm=YwPrintDocConvertor.convertVoToForm(doc);
                ywPrintDocDao.save(doc);
            }
        }
       return null;
    }
    
    
    private WfTemplateAttachmentRef getAttach(YwPrintDocForm form,String templateId){
        WfTemplateAttachmentRef entity=null;
        if(StringUtils.isNotBlank(templateId)){
            String sql=" from "+WfTemplateAttachmentRef.class.getName()+" where templateId='"+Long.valueOf(templateId)+"'" +" and fileCode='"+form.getFileCode()+"'";
            List<WfTemplateAttachmentRef> forms=ywPrintDocDao.find(sql);
            if(forms!=null && forms.size()>0){
                 entity=forms.get(0);
               
            }
        }
       return entity;
    }

    public YwPrintDocForm setForm(YwPrintDocForm form,String templateId) {
        WfTemplateAttachmentRef entity=this.getAttach(form, templateId);
        if(entity!=null){
            form.setTempdocId(entity.getId());   
        }
        return form;
    }

    /**
     * 根据templateCode和fileCore/ywId查找文书
     */
    public YwPrintDocForm getSltjFile(String templateCode,String fileCoe,Long ywId) {
    	YwPrintDocForm ywPrintDocForm=null;
    	List<YwPrintDocForm> ywPrintDocFormList = this.get(templateCode,fileCoe,ywId);
        if(ywPrintDocFormList!=null&&ywPrintDocFormList.size()>0){
        	 ywPrintDocForm=ywPrintDocFormList.get(0);       	
        }
        return ywPrintDocForm;
    }
    
	@Override
	public String updateQuAcceptNo(String templateCode, String masterEntityKey,String acceptNo) {
		Long templateId=this.getTemplateIdByCode(templateCode);
		if(templateId==null){
			return null;
		}
		String sql=" from WfBusInstance where masterEntityKey='"+masterEntityKey+"' and templateId="+
		  templateId;
		String area=acceptNo.substring(0,2);
		if("从化".equals(area)||"增城".equals(area)){
			area+="市";
		}else{
			area+="区";
		}
		
		//查询工作流表的数据
		List<WfBusInstance> inst=wfBusInstanceDao.find(sql);
		if(inst.size()>0){
			WfBusInstance instance=inst.get(0);
			if(instance.getBusDate4()!=null){
				return null;//说明已经更新了受理号
			}
			//开始查询受理号
			String hql=" select count(*) as count from WF_BUS_INSTANCE where bus_memo2='"+area+"'"+ 
						" and to_char(bus_date2,'yyyy-MM-dd')='"+DateUtilsEX.dateFormate(new Date(), "yyyy-MM-dd")+"'"
						+" and bus_date4 is not null";
			List list=wfBusInstanceDao.queryForList(hql);
			Map map=(Map) list.get(0);
			String val=(String) map.get("count");
			int newval=Integer.valueOf(val)+1;
			String numstr=acceptNo.substring(0,10);//截取到日期、
			String fix="";
			if(String.valueOf(newval).length()<3){
				for(int k=1;k<=(3-String.valueOf(newval).length());k++){
					fix+="0";
				}
			}
			acceptNo=numstr+fix+newval;//得到了新的受理号
			if(StringUtils.isNotBlank(acceptNo)){
				this.updateAcceptNo(templateCode, masterEntityKey, acceptNo);
			}
			//将受理号更新到工作流表
			if(StringUtils.isNotBlank(acceptNo) && instance!=null){
					instance.setBusMemo3(acceptNo);
					instance.setBusDate4(new Date());//生成受理号日期
					wfBusInstanceDao.save(instance);
					log.info("重新修改受理号------------"+acceptNo);
			}
		}
			
		return acceptNo;
	}
		
		/**
		 * 获取模版id
		 * @param templateCode
		 * @return
		 */
	private Long getTemplateIdByCode(String templateCode){
		String templatehql=" from WfBusTemplate where templateCode='"+templateCode+"'";
		List templates=wfBusInstanceDao.find(templatehql);
		if(templates.size()>0){
			WfBusTemplate template=(WfBusTemplate) templates.get(0);
			Long templateId=template.getId();
			if("环卫".equals(template.getTemplateName())){//环卫占时不更改申请号
				return null;
			}
			return templateId;
		}
		return null;
	}
		
    /*
	 * (non-Javadoc)
	 * @see com.augurit.gzcgwsp.wfex.bi.service.IWfBusTemplateServiceEX#updateAcceptNo(java.lang.String, java.lang.String)
	 */
	@Override
	public String updateAcceptNo(String templatecode, String masterEntityKey) {
		String templateCode="";
		String serialNo="";
		WfBusInstance instance=null;//在工作流表中查询是否有更新过受理号
		String templatehql=" from WfBusTemplate where templateCode='"+templatecode+"'";
		List templates=wfBusInstanceDao.find(templatehql);
		if(templates.size()>0){
			WfBusTemplate template=(WfBusTemplate) templates.get(0);
			Long templateId=template.getId();
			String sql=" from WfBusInstance where masterEntityKey='"+masterEntityKey+"' and templateId="+
			  templateId;
			List<WfBusInstance> inst=wfBusInstanceDao.find(sql);
			if(inst.size()>0){
				instance=inst.get(0);
				if(instance.getBusDate4()!=null){
					return null;
			}
			
		//先查询是否属于政务中心数据交换的业务
		if(!"rqjyxkz_pzqy".equals(templatecode) && !"rqjyxkz_gdqy".equals(templatecode)){
			//余泥处不能直接查询
			String[] temp=templatecode.split("_");
				templateCode=temp[0];
			
		}else{
			templateCode=templatecode;
		}
		String hql=" from ZhengwuTemplatecode where templatecode='"+templateCode+"'";
		List list=ywPrintDocDao.find(hql);
		//查询完后赋值回去，后面要用到变量
		templateCode=templatecode;
		Map<String,String> map = null;
		if(list.size()>0){
			//重新生成受理号
			 Map<String,String>  formMap=this.getMapByTemplate(templateCode, masterEntityKey);
        	if("Y".equals(formMap.get("sendNotice"))){//网办，1
        		map = ywMtbjlshInfoService.getRealSerialNo("1");
        	}else{//大厅，0
        		map = ywMtbjlshInfoService.getRealSerialNo("0");
        	}
        	serialNo = map.get("serialNo");
		}
		
		if(StringUtils.isNotBlank(serialNo)){
			this.updateAcceptNo(templateCode, masterEntityKey, serialNo);
		}
		
		//将受理号更新到工作流表
		if(StringUtils.isNotBlank(serialNo) && instance!=null){
				instance.setBusMemo3(serialNo);
				instance.setBusDate4(new Date());//生成受理号日期
				wfBusInstanceDao.save(instance);
				log.info("重新修改受理号------------"+serialNo);
		}
	  }
	 }
		return serialNo;
	}
	
	/**
	 * 更新受理号
	 * @param templateCode
	 * @param masterEntityKey
	 * @param serialNo
	 */
	private void updateAcceptNo(String templateCode,String  masterEntityKey,String serialNo){
		if ("rqssgd".equals(templateCode)) {//设施改动
			YwRqSsgdSqb form = ywRqSsgdSqbService.getEntity(Long.valueOf(masterEntityKey));
			form.setSerialNo(serialNo);
			form.setAcceptNo(serialNo);
			ywRqSsgdSqbService.updateAcceptNo(form);
		}else if ("rqrsqjazwxqyzz".equals(templateCode)) {//企业资质
			YwRqQyzzInfo entity = ywRqQyzzInfoService.getEntity(Long.valueOf(masterEntityKey));
			entity.setSerialNo(serialNo);
			entity.setAcceptNo(serialNo);
			ywRqQyzzInfoService.updateAcceptNo(entity);//.saveEntity(entity);
		}else if("hwggshsqb".equals(templateCode)){//户外广告	
			YwHwShSqb form = ywHwShSqbService.getEntity(Long.valueOf(masterEntityKey));
			form.setSerialNo(serialNo);
			form.setAcceptNo(serialNo);
			ywHwShSqbService.updateAcceptNo(form);//.saveEntity(form);
		}else if(templateCode.contains("rqjyxkz")){		//经营许可--管道和瓶装	
			YwRqQyjyxk entity = ywRqQyjyxkService.getEntity(Long.valueOf(masterEntityKey));
			entity.setSerialNo(serialNo);
			entity.setAcceptNo(serialNo);
			ywRqQyjyxkService.updateAcceptNo(entity);
		}else if("csdllszysp".equals(templateCode)||"csdllszyspqx".equals(templateCode)){//城市道路人行道及相关公共场地临时占用审批 
				YwSrDlzy form = ywDlzyService.getEntity(Long.valueOf(masterEntityKey));
				form.setSerialNo(serialNo);
				form.setAcceptNo(serialNo);
				ywDlzyService.updateAcceptNo(form);
		}else if("csdlwj".equals(templateCode)||"csdlwjqx".equals(templateCode)){//城市道路挖掘
			YwSrCsdlwj form = ywCsdlwjService.getEntity(Long.valueOf(masterEntityKey));
			form.setSerialNo(serialNo);
			form.setAcceptNo(serialNo);
			ywCsdlwjService.updateAcceptNo(form);
		}else if("ccgbggcs".equals(templateCode)){//拆除、关闭公厕许可
			YwGgcs form = ywGgcsServices.getEntity(Long.valueOf(masterEntityKey));
			form.setSerialNo(serialNo);
			form.setAcceptNo(serialNo);
			ywGgcsServices.updateAcceptNo(form);
		}else if("shljqssj".equals(templateCode)){//从事陆域城市生活垃圾经营性清扫、收集、运输服务许可审批
			Object form = ywShljqsService.get(Long.valueOf(masterEntityKey));
		}else if("shljys".equals(templateCode)){//从事水域城市生活垃圾经营性清扫、收集、运输服务许可审批
			Object form = shljYsService.get(Long.valueOf(masterEntityKey));
		}else if("shljcz".equals(templateCode)){//从事城市生活垃圾经营性处置服务许可审批
			Csshljcz form = csshljCzService.getEntity(Long.valueOf(masterEntityKey));
			form.setSerialNo(serialNo);
			form.setAcceptNo(serialNo);
			csshljCzService.updateAcceptNo(form);
		}else if(templateCode.contains("jzfqwczzpf")){//余泥处 排放  新增、变更、延期
			if("jzfqwczzpf_xz".equals(templateCode)){
				YwYnJzfqwczzPfsn  form = ywYnJzfqwczzPfsnService.getEntity(Long.valueOf(masterEntityKey));
				form.setSerialNo(serialNo);
				form.setAcceptNo(serialNo);
				ywYnJzfqwczzPfsnService.updateAcceptNo(form);
			}else if("jzfqwczzpf_bg".equals(templateCode)){
				YwYnJzfqwczzPfsn form = ywYnJzfqwczzPfsnService.getEntity(Long.valueOf(masterEntityKey));
				form.setSerialNo(serialNo);
				form.setAcceptNo(serialNo);
				ywYnJzfqwczzPfsnService.updateAcceptNo(form);
			}else{
				YwYnJzfqwczzPfsn form = ywYnJzfqwczzPfsnService.getEntity(Long.valueOf(masterEntityKey));
				form.setSerialNo(serialNo);
				form.setAcceptNo(serialNo);
				ywYnJzfqwczzPfsnService.updateAcceptNo(form);
			}
		}else if(templateCode.contains("jzfqwczzxn")){//余泥处 消纳  新增、变更、延期
			if("jzfqwczzxn_xz".equals(templateCode)){
				YwYnJzfqwczzXn form = ywYnJzfqwczzXnService.getEntity(Long.valueOf(masterEntityKey));
				form.setSerialNo(serialNo);
				form.setAcceptNo(serialNo);
				ywYnJzfqwczzXnService.updateAcceptNo(form);
			}else if("jzfqwczzxn_bg".equals(templateCode)){
				YwYnJzfqwczzXn form = ywYnJzfqwczzXnService.getEntity(Long.valueOf(masterEntityKey));
				form.setSerialNo(serialNo);
				form.setAcceptNo(serialNo);
				ywYnJzfqwczzXnService.updateAcceptNo(form);
			}else{
				YwYnJzfqwczzXn form = ywYnJzfqwczzXnService.getEntity(Long.valueOf(masterEntityKey));
				form.setSerialNo(serialNo);
				form.setAcceptNo(serialNo);
				ywYnJzfqwczzXnService.updateAcceptNo(form);
			}
		}else if(templateCode.contains("jzljczz")){//余泥处 建筑废弃物处置证(运输)==>  新增发证、年审续证、变更换证、损坏换证、遗失补办
			if("jzljczz_ysbb".equals(templateCode)){
				YwYnJzfqwczcJbxx form = ywYnJzfqwczcJbxxService.getEntity(Long.valueOf(masterEntityKey));
				form.setSerialNo(serialNo);
				form.setAcceptNo(serialNo);
				ywYnJzfqwczcJbxxService.updateAcceptNo(form);
			}else if("jzljczz_xzfz".equals(templateCode)){
				YwYnJzfqwczcJbxx form = ywYnJzfqwczcJbxxService.getEntity(Long.valueOf(masterEntityKey));
				form.setSerialNo(serialNo);
				form.setAcceptNo(serialNo);
				ywYnJzfqwczcJbxxService.updateAcceptNo(form);
			}else if("jzljczz_shhz".equals(templateCode)){
				YwYnJzfqwczcJbxx form = ywYnJzfqwczcJbxxService.getEntity(Long.valueOf(masterEntityKey));
				form.setSerialNo(serialNo);
				form.setAcceptNo(serialNo);
				ywYnJzfqwczcJbxxService.updateAcceptNo(form);
			}else if("jzljczz_nsxz".equals(templateCode)){
				YwYnJzfqwczcJbxx form = ywYnJzfqwczcJbxxService.getEntity(Long.valueOf(masterEntityKey));
				form.setSerialNo(serialNo);
				form.setAcceptNo(serialNo);
				ywYnJzfqwczcJbxxService.updateAcceptNo(form);
			}else{
				YwYnJzfqwczcJbxx form = ywYnJzfqwczcJbxxService.getEntity(Long.valueOf(masterEntityKey));
				form.setSerialNo(serialNo);
				form.setAcceptNo(serialNo);
				ywYnJzfqwczcJbxxService.updateAcceptNo(form);
			}
		}
	
	}
	
	/**
	 * 修改答复时间
	 * @param form
	 */
	private void updateDate(YwPrintDocForm form) {
		String tempCode=form.getTemplateCode();
		Long ywid=form.getYwId();
		String hql=" select proc from WfBusInstance inst,Jbpm4HistProcinst proc,WfBusTemplate temp where inst.procInstId=proc.procInstId " +
				" and inst.masterEntityKey='"+ywid+"' and inst.templateId=temp.id and temp.templateCode='"+tempCode+"'";
		List list=this.ywPrintDocDao.find(hql);
		Jbpm4HistProcinst proc=(Jbpm4HistProcinst) list.get(0);
		String id=proc.getProcInstId();
		String taskhql="from Jbpm4Task where executionId='"+id+"'";
		List tasks=jbpm4HistProcinstDao.find(taskhql);
		Date newdate=DateUtilsEX.getDateByDay(new Date(), 30);
		if(tasks.size()>0){
			Jbpm4Task task=(Jbpm4Task) tasks.get(0);
			String durationExpression = wfServiceEX.getProcDueExpressIonByTaskInstDbid(task.getTaskInstDbid());
			String[] days=durationExpression.split(" ");
			int workDays=0;
			if(days.length>0){
				workDays=Integer.valueOf(days[0]);
				newdate=wfServiceEX.calculateDueDate(newdate,workDays);
			}    			
		}		
		proc.setProcessDuedate(newdate);
		jbpm4HistProcinstDao.save(proc);//更新工作流日期
	}

	@Override
	public boolean checkDate(String ywId, String templateCode, String fileCode) {
		Map map=this.getMapByTemplate(templateCode, ywId);
		String bjgzqd=(String)map.get("bjgzqd");//材料清单
		String bzyy=(String)map.get("bjgzyy");//告知原因
		if("bzcltzs".equals(fileCode)){
			if((StringUtils.isBlank(bjgzqd)) || (StringUtils.isBlank(bzyy) )){
				return false;
			}
		}else if("hwggshsqb".equals(templateCode) && "hwggbzcltzs".equals(fileCode)){
			if((StringUtils.isBlank(bjgzqd)) || (StringUtils.isBlank(bzyy) )){
				return false;
			}
		}else if(templateCode.indexOf("jzljczz_")>=0 && "ywjczgtzs".equals(fileCode)){
			//建筑垃圾处置证运输-整改通知书
			if((StringUtils.isBlank(bjgzqd)) || (StringUtils.isBlank(bzyy) )){
				return false;
			}
		}else if(templateCode.indexOf("jzljczz_")>=0 && "ywjczgtzs".equals(fileCode)){
			//建筑垃圾处置证运输-整改通知书
			if((StringUtils.isBlank(bjgzqd)) || (StringUtils.isBlank(bzyy) )){
				return false;
			}
		} else if( "xzxksqclbzgzs".equals(fileCode) && "csdllszysp".equals(templateCode)){
			//道路占用
			if((StringUtils.isBlank(bjgzqd)) || (StringUtils.isBlank(bzyy) )){
				return false;
			}
		}
	   
		return true;
	}
	/**
	 * 
	 * @param templateCode 业务模板
	 * @param masterEntityKey 主键id
	 * @return
	 */
	public  Map<String,String> getMapByTemplate(String templateCode,String masterEntityKey){
		Object form=null;
		if (templateCode.contains("rqssgd")) {//设施改动
			form = ywRqSsgdSqbService.get(Long.valueOf(masterEntityKey));
		}else if (templateCode.contains("rqrsqjazwxqyzz")) {//企业资质
			form = ywRqQyzzInfoService.get(Long.valueOf(masterEntityKey));
		}else if("hwggshsqb".equals(templateCode)){//户外广告	
			form = ywHwShSqbService.get(Long.valueOf(masterEntityKey));
		}else if(templateCode.contains("rqjyxkz")){		//经营许可--管道和瓶装	
			form = ywRqQyjyxkService.get(Long.valueOf(masterEntityKey));			
		}else if(templateCode.contains("csdllszysp")){//城市道路人行道及相关公共场地临时占用审批 
			form = ywDlzyService.get(Long.valueOf(masterEntityKey));
		}else if(templateCode.contains("csdlwj")){//城市道路挖掘
			form = ywCsdlwjService.get(Long.valueOf(masterEntityKey));
		}else if("ccgbggcs".equals(templateCode)){//拆除、关闭公厕许可
			form = ywGgcsServices.get(Long.valueOf(masterEntityKey));
		}else if("shljqssj".equals(templateCode)){//从事陆域城市生活垃圾经营性清扫、收集、运输服务许可审批
			form = ywShljqsService.get(Long.valueOf(masterEntityKey));
		}else if("shljys".equals(templateCode)){//从事水域城市生活垃圾经营性清扫、收集、运输服务许可审批
			form = shljYsService.get(Long.valueOf(masterEntityKey));
		}else if("shljcz".equals(templateCode)){//从事城市生活垃圾经营性处置服务许可审批
			form = csshljCzService.get(Long.valueOf(masterEntityKey));
		}else if(templateCode.contains("jzfqwczzpf")){//余泥处 排放  新增、变更、延期
			if("jzfqwczzpf_xz".equals(templateCode)){
				form = ywYnJzfqwczzPfsnService.get(Long.valueOf(masterEntityKey));
			}else if("jzfqwczzpf_bg".equals(templateCode)){
				form = ywYnJzfqwczzPfsnbgService.get(Long.valueOf(masterEntityKey));
			}else{
				form = ywYnJzfqwczzPfsnyqService.get(Long.valueOf(masterEntityKey));
			}
		}else if(templateCode.contains("jzfqwczzxn")){//余泥处 消纳  新增、变更、延期
			if("jzfqwczzxn_xz".equals(templateCode)){
				form = ywYnJzfqwczzXnService.get(Long.valueOf(masterEntityKey));
			}else if("jzfqwczzxn_bg".equals(templateCode)){
				form = ywYnJzfqwczzXnbgService.get(Long.valueOf(masterEntityKey));
			}else{
				form = ywYnJzfqwczzXnyqService.get(Long.valueOf(masterEntityKey));
			}
		}else if(templateCode.contains("jzljczz")){//余泥处 建筑废弃物处置证(运输)==>  新增发证、年审续证、变更换证、损坏换证、遗失补办
			if("jzljczz_ysbb".equals(templateCode)){
				form = ywYnJzfqwczcYsbbService.get(Long.valueOf(masterEntityKey));
			}else if("jzljczz_xzfz".equals(templateCode)){
				form = ywYnJzfqwczcJbxxService.get(Long.valueOf(masterEntityKey));
			}else if("jzljczz_shhz".equals(templateCode)){
				form = ywYnJzfqwczcShhzService.get(Long.valueOf(masterEntityKey));
			}else if("jzljczz_nsxz".equals(templateCode)){
				form = ywYnJzfqwczcNsxzService.get(Long.valueOf(masterEntityKey));
			}else{
				form = ywYnJzfqwczcBghzService.get(Long.valueOf(masterEntityKey));
			}
		}
		//开始补交受理
		Map<String,String> map=WfUtilsEX.buildMap(form,"yyyy年MM月dd日");
		 if(templateCode.contains("csdllszysp")||templateCode.contains("csdlwj")){
			 //市容查询补交告知 
			form = ywDlzyService.get(Long.valueOf(masterEntityKey));
			
			
		}
		
		
		
		return map;
		
	}

	/*
	 * (non-Javadoc)
	 * @see com.augurit.gzcgwsp.printDoc.service.IYwPrintDocService#saveEntity(com.augurit.gzcgwsp.printDoc.web.form.YwPrintDocForm)
	 */
	@Override
	public void saveEntity(YwPrintDocForm form) {
		this.save(form);
		YwPrintDoc ywPrintDoc = new YwPrintDoc();
		YwPrintDocConvertor.convertFormToVo(form, ywPrintDoc);
		String templateCode=ywPrintDoc.getTemplateCode();
		Long ywid=ywPrintDoc.getYwId();
		//如果补正材料通知书提交政务中心接口
		String username=ywPrintDoc.getCreator();
			if("hwggshsqb".equals(templateCode) && "hwggbzcltzs".equals(form.getFileCode())){
				//户外广告意见审查书
				exchangeStateService.saveBjgz(templateCode, String.valueOf(ywid),username);
			}else if(!"hwggshsqb".equals(templateCode) && StringUtils.isNotBlank(form.getFileCode()) 
					&& ("bzcltzs".equals(form.getFileCode()))){
			 //修改受理时间
			 updateDate(form);
			exchangeStateService.saveBjgz(templateCode, String.valueOf(ywid),username);
			}else if( "xzxksqclbzgzs".equals(form.getFileCode()) && "csdllszysp".equals(templateCode)){
				//道路占用
				 updateDate(form);
				exchangeStateService.saveBjgz(templateCode, String.valueOf(ywid),username);
			}
		}

	/*
	 * (non-Javadoc)
	 * @see com.augurit.gzcgwsp.printDoc.service.IYwPrintDocService#getFile(java.lang.String, long, java.lang.String)
	 */
	@Override
	public List<YwPrintDocForm> getFile(String templateCode, long ywId,
			String fileCode) {
		String hql = "from YwPrintDoc t where t.templateCode =:templateCode and t.ywId =:ywId and t.fileCode=:fileCode";
		
		//初始化相应的条件参数
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("templateCode", templateCode);
		params.put("ywId", ywId);
		params.put("fileCode", fileCode);
		List<YwPrintDoc> ywPrintDocList = ywPrintDocDao.find(hql, params);		
		return YwPrintDocConvertor.convertVoListToFormList(ywPrintDocList);
		//return null;
	}

	@Override
	public List getFileType(Map param) {
		String sql=" select distinct LTRIM(RTRIM(t.memo)) from wf_template_attachment_ref t" +
				", wf_bus_template temp where t.template_id=temp.id and  t.memo is not null";
		if(param!=null){
				String templateCode=(String) param.get("templateCode");
				if(StringUtils.isNotBlank(templateCode)){
					sql+=" and temp.template_code='"+templateCode+"'";
				}
				String fileCode=(String) param.get("fileCode");
				if(StringUtils.isNotBlank(fileCode)){
					sql+=" and t.file_code='"+fileCode+"'";
				}
		}
		
		sql+=" order by LTRIM(RTRIM(t.memo)) ";
		
		Query query=this.ywPrintDocDao.getSession().createSQLQuery(sql);
		List list=query.list();
		return list;
	}
	/**
	 * 生成文号的sql
	 * @return
	 */
	private String findFileNumSql(Map map){
		String fileyear=(String) map.get("fileyear");
		String fileNum=(String) map.get("fileNum");
		String filetype=(String) map.get("filetype");
		// 查询语句及参数
		StringBuffer hql = new StringBuffer("select doc,inst.busMemo1,inst.busMemo3 from YwPrintDoc doc, ");
		hql.append(" WfBusInstance inst,WfBusTemplate temp where temp.id=inst.templateId");
		hql.append(" and temp.templateCode=doc.templateCode and inst.masterEntityKey=doc.ywId ");
		hql.append(" and doc.fileNum is not null and doc.fileCode!='sjtzs'");
		if(StringUtils.isNotBlank(fileyear)){
			hql.append(" and to_char(doc.creatDate,'yyyy')='"+fileyear+"'");
		}
		if(StringUtils.isNotBlank(fileNum)){
			hql.append(" and doc.fileNum like '%"+fileNum+"号%'");
		}
		if(StringUtils.isNotBlank(filetype)){
			hql.append(" and doc.fileNum like'"+filetype+"%'");
		}
		
		hql.append("order by doc.creatDate desc");
		return hql.toString();
	}
	
	
	/**
	 * 生成区文号的sql
	 * @return
	 */
	private String findQuFileNumSql(Map map){
		String fileyear=(String) map.get("fileyear");
		String fileNum=(String) map.get("fileNum");
		String filetype=(String) map.get("filetype");
		String orgName=(String) map.get("orgName");
		// 查询语句及参数
		StringBuffer hql = new StringBuffer("select doc,inst.busMemo1,inst.busMemo3 from YwPrintDoc doc, ");
		hql.append(" WfBusInstance inst,WfBusTemplate temp where temp.id=inst.templateId");
		hql.append(" and temp.templateCode=doc.templateCode and inst.masterEntityKey=doc.ywId ");
		hql.append(" and doc.fileNum is not null and doc.fileCode!='sjtzs' and doc.fileCode like 'qu%'");
		if(StringUtils.isNotBlank(fileyear)){
			hql.append(" and to_char(doc.creatDate,'yyyy')='"+fileyear+"'");
		}
		if(StringUtils.isNotBlank(fileNum)){
			hql.append(" and doc.fileNum like '%"+orgName.substring(0,1)+"%'");
			hql.append(" and doc.fileNum like '%"+fileNum+"号%'");
		}
		if(StringUtils.isNotBlank(filetype)){
			hql.append(" and doc.fileNum like'"+filetype+"%'");
		}
		
		hql.append("order by doc.creatDate desc");
		return hql.toString();
	}
	
	/**
	 * 生成燃气企业登记表的sql
	 * @return
	 */
	private String findRqQyDjbSql(Map map){
		String fileyear=(String) map.get("fileyear");
		// 查询语句及参数
		StringBuffer hql = new StringBuffer("select doc,inst.busMemo1,inst.busMemo3,jbpm4HistTask.histTaskInstDbid from YwPrintDoc doc, ");
		hql.append(" WfBusInstance inst,WfBusTemplate temp,Jbpm4HistTask jbpm4HistTask where temp.id=inst.templateId and inst.procInstId=jbpm4HistTask.execution");
		hql.append(" and temp.templateCode=doc.templateCode and inst.masterEntityKey=doc.ywId and jbpm4HistTask.outcome like '%to end%'");
		hql.append(" and doc.templateCode in('rqjyxkzqx_gdqy','rqjyxkzqx_pzqy')  and doc.fileCode='qusrqqydjb'");
		if(StringUtils.isNotBlank(fileyear)){
			hql.append(" and to_char(doc.creatDate,'yyyy')='"+fileyear+"'");
		}
		
		hql.append("order by doc.creatDate desc");
		System.out.println("sql = "+hql.toString());
		return hql.toString();
	}
	
	public Page searchRqQyDjb(Page page, Map map) {
		// 建立新的分页对象
		Page pg = PageUtils.newPageInstance(page);
		String hql=findRqQyDjbSql(map);
		List values = new ArrayList();
		// 查询条件
		// 执行分页查询操作
		this.ywPrintDocDao.findPage(pg, hql, values);
		// 转换为Form对象列表并赋值到原分页对象中
		List<YwPrintDocForm> list = this.convertVoListToFormList(pg.getResult());
		
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	
	@Override
	public Page search(Page page, Map map) {
		// 建立新的分页对象
		Page pg = PageUtils.newPageInstance(page);
		String hql=findFileNumSql(map);
		List values = new ArrayList();
		// 查询条件
		// 执行分页查询操作
		this.ywPrintDocDao.findPage(pg, hql, values);
		// 转换为Form对象列表并赋值到原分页对象中
		List<YwPrintDocForm> list = this.convertVoListToFormList(pg.getResult());
		
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	
	public  List<YwPrintDocForm> convertVoListToFormList(List<Object[]> docList) {
		if(docList != null && docList.size() > 0) {
			List<YwPrintDocForm> list = new ArrayList<YwPrintDocForm>();
			for(int i=0; i<docList.size(); i++) {
				Object[] objects=docList.get(i);
				YwPrintDoc doc=(YwPrintDoc) objects[0];
				String sqdw=(String)objects[1];
				String acceptNo=(String)objects[2];
				YwPrintDocForm form=YwPrintDocConvertor.convertVoToForm(doc);
				if(objects[3]!=null){
					Long taskInstDbid = (Long)objects[3];
					form.setTaskInstDbid(taskInstDbid.toString());
				}
				form.setSqdw(sqdw);
				form.setAcceptNo(acceptNo);
				
				list.add(form);
			}
			return list;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see com.augurit.gzcgwsp.printDoc.service.IYwPrintDocService#exportExcel(java.util.Map)
	 */
	@Override
	public String exportExcel(Map map) {
		String path = (String) map.get("contextpath");
		String outFilePath = GetFileName.GetRealPath("Templates/modelTemplate",
				"xls");
		String outPath = path + File.separator + outFilePath;
		outPath = outPath.replace("/", File.separator);
		String dir = outPath.substring(0, outPath.lastIndexOf(File.separator));
		dir = dir.replace("/", File.separator);
		File dirfile = new File(dir);
		if (!dirfile.exists()) {
			dirfile.mkdirs();
		}
		String inFilePath = path + File.separator + "Templates"
				+ File.separator + "exportFileNums.xls";
		outPath = outPath.replace("/", File.separator);
		String sql ="";
		if("qu".equals(map.get("filearea"))){//如果是区
			 sql = this.findQuFileNumSql(map);
		}else{
			sql = this.findFileNumSql(map);
		}
		 
		List list = ywPrintDocDao.find(sql);
		writeData(inFilePath, outPath, list, map);
		String filename = outPath.substring(
				outPath.lastIndexOf(File.separator) + 1, outPath.length());
		try {// 删除其他文件
			FileUtil.delDir(dir, filename);
		} catch (IOException e) {
			log.error("删除文件错误" + e);
		}
		return outFilePath;
	}
	
	/**
	 * 写入查询数据
	 * 
	 * @param inFilePath
	 * @param outFilePath
	 * @param list
	 */
	private void writeData(String inFilePath, String outFilePath, List list,
			Map params) {
		WritableWorkbook wwb;
		try {
			wwb = ExcelUtil.createWorkbookByOld(inFilePath, outFilePath);
			WritableSheet ws = ExcelUtil.getWritableSheetByIndex(wwb, 0);
			WritableFont font = new WritableFont(WritableFont.createFont("宋体"),
					12);
			WritableCellFormat wcf = new WritableCellFormat(font,
					NumberFormats.TEXT);
			wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
			wcf.setAlignment(Alignment.LEFT);
			wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
			wcf.setWrap(true);
			int k=1;
			for (int i = 0; i < list.size(); i++) {
				Object[] obj=(Object[]) list.get(i);
				YwPrintDoc doc=(YwPrintDoc) obj[0];
				String sqdw=(String)obj[1];
				String acceptNo=(String)obj[2];
				ExcelUtil.modiStrCell2(ws, 0, k, acceptNo, wcf);//受理号
				ExcelUtil.modiStrCell2(ws, 1, k, doc.getFileNum(), wcf);
				ExcelUtil.modiStrCell2(ws, 2, k, sqdw, wcf);// 申请单位				
				ExcelUtil.modiStrCell2(ws, 3, k , doc.getCreator(), wcf);
				ExcelUtil.modiStrCell2(ws, 4, k , DateUtilsEX.dateFormate(doc.getCreatDate(), "yyyy-MM-dd"), wcf);// 结果
				ws.setRowView(i + 1, 400);
				k++;
		
			}
			wwb.write();
			wwb.close();
		} catch (Exception e) {

		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.augurit.gzcgwsp.printDoc.service.IYwPrintDocService#checkFileNum(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public boolean checkFileNum(String ywId, String templateCode,
			String fileCode, String fileNum) {
		
		//查询哪种文书
		String hql="select ref.memo from WfTemplateAttachmentRef  ref,WfBusTemplate temp where temp.id=ref.templateId and ref.fileCode='"+fileCode;
		hql+="' and temp.templateCode='"+templateCode+"'";
		List list=this.ywPrintDocDao.find(hql);
		if(list.size()>0){
			String memo =(String) list.get(0);
			//构造文号
			String fileNo=memo+"["+DateUtilsEX.getCurYear()+"]"+fileNum+"号";
			String sql="  from YwPrintDoc where fileNum='"+fileNo+"'";
			List docs=ywPrintDocDao.find(sql);
		     if(docs.size()>0){
		    	 return false;
		     }
				
				return true;
			
		}
		
		return true;
	}

	@Override
	public boolean checkQuFileNum(String ywId, String templateCode,
			String fileCode, String fileNum, LoginUserForm loginUser) {
		//查询哪种文书
		String hql="select ref.memo from WfTemplateAttachmentRef  ref,WfBusTemplate temp where temp.id=ref.templateId and ref.fileCode='"+fileCode;
		hql+="' and temp.templateCode='"+templateCode+"'";
		List list=this.ywPrintDocDao.find(hql);
		if(list.size()>0){
			String memo =(String) list.get(0);
			//构造文号
			String fileNo=memo+"("+loginUser.getOrg().getOrgName().substring(0,1)+")["+DateUtilsEX.getCurYear()+"]"+fileNum+"号";
			String sql="  from YwPrintDoc where fileNum='"+fileNo+"'";
			List docs=ywPrintDocDao.find(sql);
		     if(docs.size()>0){
		    	 return false;
		     }
				
				return true;
			
		}
		
		return true;
	}

	@Override
	public String checkQuData(String ywId, String templateCode, String fileCode) {
		String sql="  from YwPrintDoc where ywId="+ywId+" and templateCode='"+templateCode+"' and fileCode='"+fileCode+"'";
		List docs=ywPrintDocDao.find(sql);
	     if(docs.size()>0){
	    	 return "false";
	     }else{
	    	 Map map=this.getMapByTemplate(templateCode, ywId);
	 		String bjgzqd=(String)map.get("bjgzqd");//材料清单
	 		String bzyy=(String)map.get("bjgzyy");//告知原因
	 		if("qubzcltzs".equals(fileCode)){
	 			if((StringUtils.isBlank(bjgzqd)) || (StringUtils.isBlank(bzyy) )){
	 				return "notgzdata";
	 			}
	 		}
	     }
			return null;
		
	}

	@Override
	public Page searchQuFileNum(Page page, Map map) {
		// 建立新的分页对象
		Page pg = PageUtils.newPageInstance(page);
		String hql=findQuFileNumSql(map);
		List values = new ArrayList();
		// 查询条件
		// 执行分页查询操作
		this.ywPrintDocDao.findPage(pg, hql, values);
		// 转换为Form对象列表并赋值到原分页对象中
		List<YwPrintDocForm> list = this.convertVoListToFormList(pg.getResult());
		
		PageUtils.copy(pg, list, page);
		return page;
	}

	@Override
	public List getquFileType(Map param) {
		String sql=" select distinct LTRIM(RTRIM(t.memo)) from wf_template_attachment_ref t" +
		", wf_bus_template temp where t.template_id=temp.id and  t.memo is not null and t.file_code like 'qu%'";
		if(param!=null){
				String templateCode=(String) param.get("templateCode");
				if(StringUtils.isNotBlank(templateCode)){
					sql+=" and temp.template_code='"+templateCode+"'";
				}
				String fileCode=(String) param.get("fileCode");
				if(StringUtils.isNotBlank(fileCode)){
					sql+=" and t.file_code='"+fileCode+"'";
				}
		}
		
		sql+=" order by LTRIM(RTRIM(t.memo)) ";
		Query query=this.ywPrintDocDao.getSession().createSQLQuery(sql);
		List list=query.list();
		return list;
		
		}

	@Override
	public WfBusInstance getWfBusInstance(String templateCode, Long ywId) {
		String sql=" select t from WfBusInstance t,WfBusTemplate temp where t.templateId=temp.id and temp.templateCode='"+templateCode+"'";
				sql+=" and t.masterEntityKey="+ywId;
		List list=this.ywPrintDocDao.find(sql);
		if(list!=null){
			 return (WfBusInstance) list.get(0);
		}
		return null;
	}

	@Override
	public void savequEntity(YwPrintDocForm form) {
		this.save(form);
		YwPrintDoc ywPrintDoc = new YwPrintDoc();
		YwPrintDocConvertor.convertFormToVo(form, ywPrintDoc);
		String templateCode=ywPrintDoc.getTemplateCode();
		Long ywid=ywPrintDoc.getYwId();
		//如果补正材料通知书提交政务中心接口
		String username=ywPrintDoc.getCreator();
			if("hwggshsqb".equals(templateCode) && "hwggbzcltzs".equals(form.getFileCode())){
				//户外广告意见审查书
				//exchangeStateService.saveBjgz(templateCode, String.valueOf(ywid),username);
			}else if(!"hwggshsqb".equals(templateCode) && StringUtils.isNotBlank(form.getFileCode()) 
					&& ("qubzcltzs".equals(form.getFileCode()))){
			 //修改受理时间
			 updateDate(form);
			//exchangeStateService.saveBjgz(templateCode, String.valueOf(ywid),username);
			}else if( "xzxksqclbzgzs".equals(form.getFileCode()) && "csdllszysp".equals(templateCode)){
				//道路占用
				 updateDate(form);
				//exchangeStateService.saveBjgz(templateCode, String.valueOf(ywid),username);
			}
		
	}


	
}
